﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class CareerPitchingStatsList
    {
        #region Properties

        [DataMember]
        public UriLink PlayerItem { get; set; }
        [DataMember]
        public List<UriLink> Teams = new List<UriLink>();
        [DataMember]
        public List<int> Year = new List<int>();
        [DataMember]
        public List<int> Stint = new List<int>();
        [DataMember]
        public List<int?> W = new List<int?>();
        [DataMember]
        public List<int?> L = new List<int?>();
        [DataMember]
        public List<int?> G = new List<int?>();
        [DataMember]
        public List<int?> GS = new List<int?>();
        [DataMember]
        public List<int?> CG = new List<int?>();
        [DataMember]
        public List<int?> SHO = new List<int?>();
        [DataMember]
        public List<int?> SV = new List<int?>();
        [DataMember]
        public List<int?> IPOuts = new List<int?>();
        [DataMember]
        public List<int?> H = new List<int?>();
        [DataMember]
        public List<int?> ER = new List<int?>();
        [DataMember]
        public List<int?> HR = new List<int?>();
        [DataMember]
        public List<int?> BB = new List<int?>();
        [DataMember]
        public List<int?> SO = new List<int?>();
        [DataMember]
        public List<double?> BAOpp = new List<double?>();
        [DataMember]
        public List<double?> ERA = new List<double?>();
        [DataMember]
        public List<int?> IBB = new List<int?>();
        [DataMember]
        public List<int?> WP = new List<int?>();
        [DataMember]
        public List<int?> HBP = new List<int?>();
        [DataMember]
        public List<int?> BK = new List<int?>();
        [DataMember]
        public List<int?> BFP = new List<int?>();
        [DataMember]
        public List<int?> GF = new List<int?>();
        [DataMember]
        public List<int?> R = new List<int?>();

        #endregion

        public CareerPitchingStatsList Load(int playerid)
        {
            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var s = (
                    from p in context.People
                    from a in context.Appearances
                    from ft in context.FieldedTeams
                    from nn in context.TeamNickNames
                    from ps in context.PitchingStats
                    where a.PersonId == p.PersonId &&
                        ft.FieldedTeamId == a.TeamId &&
                        nn.TeamNickNameId == ft.TeamNickNameId &&
                        ps.TeamId == ft.FieldedTeamId &&
                        p.PersonId == playerid &&
                        ps.PersonId == p.PersonId
                    orderby ft.Yr, ps.Stint
                    select new
                    {
                        p.FirstName,
                        p.LastName,
                        p.PersonId,
                        ft.Yr,
                        nn.NickName,
                        ft.FieldedTeamId,
                        ps.BAOpp,
                        ps.BB,
                        ps.BFP,
                        ps.BK,
                        ps.CG,
                        ps.ER,
                        ps.ERA,
                        ps.G,
                        ps.GF,
                        ps.GS,
                        ps.H,
                        ps.HBP,
                        ps.HR,
                        ps.IBB,
                        ps.IPouts,
                        ps.L,
                        ps.R,
                        ps.SHO,
                        ps.SO,
                        ps.Stint,
                        ps.SV,
                        ps.W,
                        ps.WP
                    }).ToList();

                foreach (var itm in s)
                {
                    this.BAOpp.Add(itm.BAOpp);
                    this.BB.Add(itm.BB);
                    this.BFP.Add(itm.BFP);
                    this.BK.Add(itm.BK);
                    this.CG.Add(itm.CG);
                    this.ER.Add(itm.ER);
                    this.ERA.Add(itm.ERA);
                    this.G.Add(itm.G);
                    this.GF.Add(itm.GF);
                    this.GS.Add(itm.GS);
                    this.H.Add(itm.H);
                    this.HBP.Add(itm.HBP);
                    this.HR.Add(itm.HR);
                    this.IBB.Add(itm.IBB);
                    this.IPOuts.Add(itm.IPouts);
                    this.L.Add(itm.L);
                    this.R.Add(itm.R);
                    this.SHO.Add(itm.SHO);
                    this.SO.Add(itm.SO);
                    this.Stint.Add(itm.Stint);
                    this.SV.Add(itm.SV);
                    this.W.Add(itm.W);
                    this.WP.Add(itm.WP);

                    this.Year.Add(itm.Yr);
                    this.Teams.Add(new UriLink(String.Format("{0}team/{1}", Constants.baseUrl, itm.FieldedTeamId), string.Format("{0} {1}", itm.Yr, itm.NickName)));
                }

                var pi = (from p in context.People
                          where p.PersonId == playerid
                          select p).FirstOrDefault();
                this.PlayerItem = new UriLink(string.Format("{0}player/{1}", Constants.baseUrl, pi.PersonId), string.Format("{0} {1}", pi.FirstName, pi.LastName));


                return this;
            }
        }

    }
}